public with sharing class OpportunityNewOverrideController {

  private string defaultrecordTypeId;
  
  private string AASOpportunityrecordTypeId  = RecordTypeHelper.GetRecordTypeId('Opportunity', 'Advanced Ad Solutions'); 
  String ent           = ApexPages.currentPage().getParameters().get('ent'); 
  
  String accountId     = ApexPages.currentPage().getParameters().get('accid');
  String retUrl        = ApexPages.currentPage().getParameters().get('retURL');  
  
            
    public OpportunityNewOverrideController(ApexPages.StandardController controller) {
        Init();
           
    }
    
    private void Init(){
            if(retUrl == '')
                retUrl = '/006/o';
                
            RecordTypeHelper.SetRecordTypeIncaseNotSet('Opportunity');
            defaultrecordTypeId  = ApexPages.currentPage().getParameters().get('RecordType');
            
            //when user clicks on new opportnity button from an accout page, loook for the accountid
            //if selected accout id is not passed in query string ,take from returl parameter            
            if(accountId==null && retUrl.startsWith('/001')){
				//remove any querystring params in retUrl 
				integer  parameterPosition =  retUrl.indexOf('?');   
				if(parameterPosition > 0){
					retUrl = retUrl.substring(0,parameterPosition);
                    System.Debug('sree****' + retUrl);
				}
            accountId= EncodingUtil.urlDecode(retUrl,'UTF-8').replaceAll('/','');
            }
        System.Debug('Pname->' + ent + 'acc->'+accountId + '+' + retUrl + '=' + ApexPages.currentPage().getParameters().get('RecordType'));
           
    }
    
    public PageReference doRedirect() {
    
       //************ custom redirect ****************************
        //redirect to  master client selection page for  AAS profiles       
       if(AASOpportunityrecordTypeId.Contains(defaultrecordTypeId)  )   {
           System.Debug('**first' + accountId );
            PageReference p = Page.SelectMasterClient;
            p.setRedirect(true);
            p.getParameters().put('retUrl',retUrl  );
            p.getParameters().put('ent', 'Opportunity');
            p.getParameters().put('RecordType',defaultrecordTypeId );
            if(accountId != null)
              p.getParameters().put('accid',accountId );     
            return p;
       }
       
       
       
        
       //************ default new opportunity redirect  ****************************
             //all other opportunities should continue to new opportunity page
          
                String  redirectURL = '/006/e?retURL=' + retUrl + '&RecordType='+ defaultrecordTypeId + '&ent=Opportunity&nooverride=1';
                         
                if(accountId!= null){
                	redirectURL += '&accid='+accountId;
                  }          
              //Set the Page Reference of what URL will it take if it passes the criteria mentioned above 
                 PageReference createnewOpp = new PageReference(redirectURL);
                 return createnewOpp;
    }
 
  

}